﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Home.Domain.Entities;
using Home.WebUI.Areas.Admin.Models.CompanyViewModel;
using Home.WebUI.Models;
using System.Data;
using Home.WebUI.Areas.Admin.Models;
using Omu.ValueInjecter;
namespace Home.WebUI.Areas.Admin.Controllers
{
    public class CompanyController : Controller
    {
        private EFDbContext db = new EFDbContext();
        //
        // GET: /Admin/Company/

        public ActionResult Index(IndexViewModel model, int page = 1)
        {
            //页面项数
            int pageSize = 10;
            var companys = db.Companys.OrderByDescending(c => c.Sort).Skip((page - 1) * pageSize).Take(pageSize);
            if (!string.IsNullOrEmpty(model.Search))
            {
                companys = companys.Where(c => c.Title.Contains(model.Search));
            }
            model.Companys = companys;
            model.PagingInfo = new PagingInfo {CurrentPage=page,ItemsPerPage=pageSize,TotalItems=db.Companys.Count() };
            return View(model);
        }
        public ActionResult Create()
        {
            //ViewBag.ColumnTitle = CompanyTitle;
            //Company_Create model = new Company_Create();
            //model.CreateDate = DateTime.Now;

            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(CreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                Company content = new Company();
                content.InjectFrom(model);
                //Company content = ObjectMapperManager.DefaultInstance.GetMapper<CreateViewModel, Company>().Map(model);
                int? sort = db.Companys.Select(c => c.Sort).Max();
                content.Sort = sort == null ? 1 : sort+1;
                content.CreateDate = DateTime.Now;
                db.Companys.Add(content);
                if (db.SaveChanges()>0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            CreateViewModel model = new CreateViewModel();
            model.InjectFrom(db.Companys.Find(id));
            //CreateViewModel model = ObjectMapperManager.DefaultInstance.GetMapper<Company, CreateViewModel>().Map(db.Companys.Find(id));
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(CreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                //按需更新
                Company content = db.Companys.Find(model.Id);
                db.Companys.Attach(content);
                content.InjectFrom(model);
                //ObjectMapperManager.DefaultInstance.GetMapper<CreateViewModel, Company>().Map(model, content);//model的值传给content
                if (db.SaveChanges() > 0)
                {
                    TempData["Tip"] = new TipModel(1, "修改成功");
                    return RedirectToAction("Index");
                }
            }
            return View();
        }
        [HttpPost]
        public ActionResult Delete(int id)
        { 
            try            
            {        
                // TODO: Add delete logic here    
                if (Request.IsAjaxRequest())     
                {
                    Company content = db.Companys.Find(id);
                    db.Companys.Remove(content);       
                    //EntityState stateafter = db.Entry(content).State;//Deleted状态     
                    int result = db.SaveChanges();                
                    return Content(result.ToString());           
                }            
                else           
                {             
                    return Content("-1");       
                }         
            }          
            catch        
            {           
                return Content("-1");   
            }
        }

        public ActionResult Up(int id)
        {
            Company content = db.Companys.Find(id);
            Company contentup = db.Companys.Where(c => c.Sort > content.Sort).OrderBy(c => c.Sort).FirstOrDefault();
            if (contentup==null)
            {
                return Redirect(Request.UrlReferrer.ToString());
            }
            db.Companys.Attach(content);
            content.Sort = contentup.Sort;
            db.Companys.Attach(contentup);
            contentup.Sort = contentup.Sort - 1;
            db.SaveChanges();
            return Redirect(Request.UrlReferrer.ToString());
        }

        public ActionResult aaa()
        {
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
